#include <stdexcept>
#include "GUI/MainWindow.hpp"

namespace gui {

MainWindow::MainWindow(pizzeria::Reception &reception)
    : mReception(reception) {
  mOrderLine = new QLineEdit;
  mTable = new QTableWidget;
  mQuitButton = new QPushButton("Quit");
  mTable->setColumnCount(13);
  mTable->setHorizontalHeaderLabels(
      QString("#;Status;Total cooks;Busy cooks;Doe;Tomatoes;Gruyere;Ham;"
	  "Mushrooms;Steak;Eggplants;Goat cheese;Chief love").split(";"));
  mTable->resizeColumnsToContents();

  QFormLayout *topLayout = new QFormLayout;
  topLayout->addRow("Order: ", mOrderLine);

  QHBoxLayout *bottomLayout = new QHBoxLayout;
  bottomLayout->setAlignment(Qt::AlignRight);
  bottomLayout->addWidget(mQuitButton);

  QVBoxLayout *mainLayout = new QVBoxLayout;
  mainLayout->addLayout(topLayout);
  mainLayout->addWidget(mTable);
  mainLayout->addLayout(bottomLayout);
  setLayout(mainLayout);
  //setFixedSize(1051, 720);
  setFixedSize(mTable->horizontalHeader()->length() + 26, 720);
  connect(mOrderLine, SIGNAL(returnPressed()), this, SLOT(OrderReceived()));
  connect(mQuitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
}

MainWindow::~MainWindow() {
  delete mOrderLine;
}

void MainWindow::OrderReceived() {
  try {
    mReception.TakeNewOrder(mOrderLine->text().toStdString());
  }
  catch (std::invalid_argument &e) {
    QMessageBox::warning(this, "Bad order syntax",
	"An error occurred while parsing the order:\n\n" + QString(e.what()));
  }
  mOrderLine->setText("");
}

} /* !namespace gui */
